<template>
  <div class="manager_post">
    <section class="manager_post_header">
      <a-card>
        <a-form name="advanced_search" autocomplete="off" ref="searchFormRef" class="ant-advanced-search-form"
          :model="searchFormState">
          <a-row :gutter="24">
            <a-col :span="12">
              <a-form-item name="title" label="标题">
                <a-input v-model:value="searchFormState.title" placeholder="请输入帖子标题" allowClear />
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item name="content" label="内容">
                <a-input v-model:value="searchFormState.content" placeholder="请输入帖子内容" allowClear />
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item name="tag" label="标签">
                <a-input v-model:value="searchFormState.tag" placeholder="请输入帖子标签" allowClear />
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item name="status" label="状态">
                <a-radio-group v-model:value="searchFormState.status" name="status">
                  <a-radio :value="1">待审核</a-radio>
                  <a-radio :value="2">审核通过</a-radio>
                  <a-radio :value="3">未通过</a-radio>
                </a-radio-group>
              </a-form-item>
            </a-col>
          </a-row>
          <a-row>
            <a-col :span="24" style="text-align: right">
              <a-button type="primary" @click="handleSearch">查询</a-button>
              <a-button style="margin: 0 8px" @click="handleReset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </a-card>
    </section>
    <section class="manager_post_content">
      <a-card style="height: 100%">
        <a-row style="margin-top: 8px;">
          <a-col :span="24">
            <a-table :dataSource="dataSource" :columns="columns" size="small" :pagination="false" :scroll="{ x: 1000 }">

              <template #bodyCell="{ column, record }">
                <template v-if="column.dataIndex === 'cover'">
                  <a-image :src="record?.cover" :width="100" :height="60" v-if="record?.cover" />
                </template>
                <template v-if="column.dataIndex === 'status'">
                  <a-badge status="warning" v-if="record.status === 1" text="待审核" />
                  <a-badge status="success" v-if="record.status === 2" text="审核通过" />
                  <a-badge status="error" v-if="record.status === 3" text="未通过" />
                </template>
                <a-space v-if="column.dataIndex === 'operation'">
                  <a-button type="link" @click="handleAudit(record)" :disabled=" record.status !== 1">审核</a-button>
                  <a-popconfirm title="确定要删除这条数据吗？" ok-text="确定" cancel-text="取消" @confirm="handleDelete(record.id)">
                    <a-button type="link" danger>删除</a-button>
                  </a-popconfirm>

                </a-space>
              </template>
            </a-table>
          </a-col>
        </a-row>
      </a-card>
    </section>
    <footer class="manager_post_footer">
      <a-card style="height: 100%;">
        <a-row align="middle">
          <a-col :span="24">
            <a-pagination :total="total" v-model:current="current" v-model:pageSize="pageSize" :showSizeChanger="true"
              showQuickJumper :show-total="total => `共 ${total} 条数据`" @change="onPageChange" />
          </a-col>
        </a-row>
      </a-card>
    </footer>
  </div>

  <a-modal v-model:open="open" title="审核帖子" @ok="handleOk" :confirm-loading="confirmLoading" :destroyOnClose="true"
      centered :maskClosable="false" @cancel="open = false">

      <a-form  :model="formState" autocomplete="off">
        <a-form-item name="status" label="状态">
                <a-radio-group v-model:value="formState.status" name="status">
                  <a-radio :value="1">待审核</a-radio>
                  <a-radio :value="2">审核通过</a-radio>
                  <a-radio :value="3">未通过</a-radio>
                </a-radio-group>
              </a-form-item>
      </a-form>
    </a-modal>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { message } from 'ant-design-vue';
import { getPostList, deletePost,editPost } from '@/apis/post';
const searchFormRef = ref();
const dataSource = ref([]);
const searchFormState = ref({
  title: "",
  content: "",
  status: null,
  tag: ""
});


const formState= ref({
  title: "",
  content: "",
  status: null,
  tag: ""
});

const confirmLoading = ref(false);

const open = ref(false);
const current = ref(1);
const total = ref(0);
const pageSize = ref(10);
const handleSearch = async () => {
  try {
    const { data = {} } = await getPostList({
      pageNum: current.value,
      pageSize: pageSize.value,
      title: searchFormState.value.title,
      content: searchFormState.value.content,
      status: searchFormState.value.status,
      tag: searchFormState.value.tag,
    });
    dataSource.value = data?.list;
    total.value = data?.total;
  } catch (error) {
    console.log(error, 'get-post-list-error');
  }

};
const onPageChange = (page, Size) => {
  current.value = page;
  pageSize.value = Size;
  init();
};

const handleReset = () => {
  searchFormRef.value?.resetFields();
  init();
};

const handleDelete = async (id) => {
  await deletePost(id);
  init();
  message.success('操作成功');
};

const handleAudit = (item) => {
  open.value = true;
  console.log(item,'item');
  formState.value = item;
};


const handleOk = async () => {
  try {
      confirmLoading.value = true;
      const res = await editPost(formState.value);
      if(res?.code === 200){
        confirmLoading.value = false;
        message.success('操作成功');
        open.value = false;
        init();
        return;
      }
      message.info(res?.msg || '操作失败');
      confirmLoading.value = false;
  } catch (error) {
    confirmLoading.value = false;
    console.log(error, 'handleOk-error');

  }
};

const init = async () => {
  try {
    const { data = {} } = await getPostList({
      pageNum: current.value,
      pageSize: pageSize.value,
    });
    dataSource.value = data?.list;
    total.value = data?.total;
  } catch (error) {
    console.log(error, 'get-post-list-error');
  }
};

onMounted(() => {
  init();
});


const columns = [
  {
    title: '帖子图片',
    dataIndex: 'cover',
    width: 160,
    align: 'center',
  },
  {
    title: '标题',
    dataIndex: 'title',
    width: 160,
    align: 'center',
  },
  {
    title: '内容',
    dataIndex: 'content',
    width: 220,
    align: 'center',
    ellipsis: true
  },
  {
    title: '标签',
    dataIndex: 'tag',
    width: 180,
    align: 'center',
  },
  {
    title: '点击量',
    dataIndex: 'clickNum',
    width: 160,
    align: 'center',
  },
  {
    title: '点赞量',
    dataIndex: 'likeNum',
    width: 160,
    align: 'center',
  },
  {
    title: '收藏量',
    dataIndex: 'collectNum',
    width: 160,
    align: 'center',
  },
  {
    title: '评论量',
    dataIndex: 'commentNum',
    width: 160,
    align: 'center',
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 100,
    align: 'center',
  },
  {
    title: '操作',
    dataIndex: 'operation',
    fixed: 'right',
    width: 140,
    align: 'center'
  },
];
</script>

<style lang="less" scoped>
.manager_post {
  box-sizing: border-box;
  padding: 8px;
  display: flex;
  flex-direction: column;
  height: 100%;

  &_header {
    width: 100%;
    max-height: 200px;
  }

  &_content {
    margin-top: 8px;
    flex: 1;
    overflow: auto;
  }

  &_footer {
    margin-top: 8px;
    height: 80px;
  }
}
</style>
